package com.mysoft.gfyx.marketdevelop.controller;

import com.mysoft.framework.modeling.dataapi.DataApi;
import com.mysoft.framework.mvc.api.Controller;
import com.mysoft.framework.rpc.annotation.PubAction;
import com.mysoft.framework.rpc.annotation.PubService;
import com.mysoft.framework.rpc.contants.RequestPrefix;
import com.mysoft.gfyx.marketdevelop.remoteService.RemoteDataPermissionService;
import com.mysoft.gfyx.marketdevelop.remoteService.RemoteProfitProvinceService;
import com.mysoft.gfyx.marketdevelop.remoteService.dto.GetProfitProvinceDataDto;
import com.mysoft.gfyx.marketdevelop.remoteService.dto.GetUserPermissionAreaResponseDto;
import com.mysoft.gfyx.marketdevelop.service.ProvinceAndCityService;
import com.mysoft.gfyx.marketdevelop.service.dto.ProvinceAndCityConditionDto;
import com.mysoft.gfyx.marketdevelop.service.dto.ProvinceAndCityDto;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 高级筛选控制器
 */
@PubService(value = "/provinceAndCityDataApi", prefix = RequestPrefix.API)
@Tag(name = "省市-API")
public class ProvinceAndCityDataApiController extends Controller implements DataApi {
    @Resource
    private ProvinceAndCityService provinceAndCityService;
    @Resource
    private RemoteProfitProvinceService remoteProfitService;
    @Resource
    private RemoteDataPermissionService remoteDataPermissionService;

    @PubAction(value = "/getAll", method = RequestMethod.POST)
    public List<ProvinceAndCityDto> getAll() {
        return provinceAndCityService.getAll();
    }


    @PubAction(value = "/getProfitAndPermissionArea", method = RequestMethod.POST)
    public List<ProvinceAndCityDto> getProfitAndPermissionArea() {
        // todo 下线该接口
        GetProfitProvinceDataDto provinceList = remoteProfitService.getProfitProvince();
        // 用户拥有的权限地区（只包含省）
        GetUserPermissionAreaResponseDto userProvincePermissionDto = remoteDataPermissionService.getUserPermissionArea();

        List<String> provincesCode = new ArrayList<>();
        if (provinceList.getProvinceNumType().equals(RemoteProfitProvinceService.ProvinceNumType.Province.toString())) {//如果是省份权益，则过滤省份
            if (userProvincePermissionDto == null
                    || userProvincePermissionDto.getArea() == null
                    || userProvincePermissionDto.getArea().contains("0")
                    || userProvincePermissionDto.getArea().size() == 0) {
                //用户有全国权益\或者没有设置权益
                provinceList.getProvinceList().forEach(m -> {
                    provincesCode.add(m.getValue());
                });
            } else {
                provinceList.getProvinceList().forEach(m -> {
                    if (userProvincePermissionDto.getArea().contains(m.getValue())) {
                        //取权益和权限的交集
                        provincesCode.add(m.getValue());
                    }
                });
            }

            if (provincesCode.size() == 0) {
                return (new ArrayList<>());
            }
        } else {
            //全国版只过滤权限省份
            if (userProvincePermissionDto == null
                    || userProvincePermissionDto.getArea() == null
                    || userProvincePermissionDto.getArea().contains("0")
                    || userProvincePermissionDto.getArea().size() == 0) {
                //用户有全国权益\或者没有设置权益
                ProvinceAndCityConditionDto dto = new ProvinceAndCityConditionDto();
                List<ProvinceAndCityDto> provinceALl = provinceAndCityService.getProvince(dto);
                provinceALl.forEach(m -> {
                    provincesCode.add(m.getItemCode());
                });
            } else {
                provincesCode.addAll(userProvincePermissionDto.getArea());
            }
        }
        return provinceAndCityService.getCityAndProvinceCode(provincesCode);
    }
}
